function findMedianSortedArrays(nums1, nums2) {
    let length1 = nums1.length, length2 = nums2.length, totalLength = length1 + length2;
    if (totalLength % 2 === 1) {
        // 找到第几大的数，若为13，找到第七大的数
        return getKthDigit(nums1, nums2, length1, length2, (totalLength + 1) / 2)
    } else {
        // 找到第几大的数，若为6，找到第三、四大的数
        return (getKthDigit(nums1, nums2, length1, length2, totalLength / 2) + getKthDigit(nums1, nums2, length1, length2, totalLength / 2 + 1)) / 2
    }
};

function getKthDigit(nums1, nums2, length1, length2, k) {
    let curIndex1 = 0, curIndex2 = 0, mid = 0, realIndex1 = 0, realIndex2 = 0

    while (true) {
        if (curIndex1 === length1) {
            return nums2[curIndex2 + k - 1]
        }
        if (curIndex2 === length2) {
            return nums1[curIndex1 + k - 1]
        }
        if (k === 1) {
            return Math.min(nums1[curIndex1], nums2[curIndex2])
        }

        // 每个数组的要取的比较的位数
        mid = Math.floor(k / 2) - 1

        // 每个数组的要取的比较的位数，和数组长度取小
        realIndex1 = Math.min(mid + curIndex1, length1 - 1)
        realIndex2 = Math.min(mid + curIndex2, length2 - 1)

        if (nums1[realIndex1] <= nums2[realIndex2]) {
            // 减去已比较的小的位数
            k -= realIndex1 - curIndex1 + 1
            // 移动指针，至已比较的数的后一位
            curIndex1 = realIndex1 + 1
        } else {
            k -= realIndex2 - curIndex2 + 1
            curIndex2 = realIndex2 + 1
        }
    }
}